package com.itheima.service.db.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.domain.CmsPrefrenceAreaProductRelation;
import com.itheima.mapper.CmsPrefrenceAreaProductRelationMapper;
import com.itheima.service.db.CmsPrefrenceAreaProductRelationService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class CmsPrefrenceAreaProductRelationServiceImpl implements CmsPrefrenceAreaProductRelationService {
    @Autowired
    CmsPrefrenceAreaProductRelationMapper cmsPrefrenceAreaProductRelationMapper;

    @Override
    public List<CmsPrefrenceAreaProductRelation> selectByProductId(Integer id) {
        LambdaQueryWrapper<CmsPrefrenceAreaProductRelation> lqw=new LambdaQueryWrapper<>();
        lqw.eq(CmsPrefrenceAreaProductRelation::getProductId,id);
        return cmsPrefrenceAreaProductRelationMapper.selectList(lqw);
    }

    @Override
    public void updateByProductId(Integer id, List<CmsPrefrenceAreaProductRelation> cmsPrefrenceAreaProductRelationList) {
        LambdaQueryWrapper<CmsPrefrenceAreaProductRelation> lqw=new LambdaQueryWrapper<>();
        lqw.eq(CmsPrefrenceAreaProductRelation::getProductId,id);
        cmsPrefrenceAreaProductRelationMapper.delete(lqw);

        for (CmsPrefrenceAreaProductRelation cmsPrefrenceAreaProductRelation : cmsPrefrenceAreaProductRelationList) {
            cmsPrefrenceAreaProductRelation.setProductId(id.longValue());
            cmsPrefrenceAreaProductRelationMapper.insert(cmsPrefrenceAreaProductRelation);
        }

    }

    @Override
    public void save(Long productId, List<CmsPrefrenceAreaProductRelation> cmsPrefrenceAreaProductRelationList) {
        for (CmsPrefrenceAreaProductRelation cmsPrefrenceAreaProductRelation : cmsPrefrenceAreaProductRelationList) {
            cmsPrefrenceAreaProductRelation.setProductId(productId);
            cmsPrefrenceAreaProductRelationMapper.insert(cmsPrefrenceAreaProductRelation);
        }
    }
}
